<?php

require_once 'AbstractPicSync.php';
/**
 * @see Zend_Loader
 */
require_once 'Zend/Loader.php';

/**
 * @see Zend_Gdata
 */
Zend_Loader::loadClass('Zend_Gdata');

/**
 * @see Zend_Gdata_AuthSub
 */
Zend_Loader::loadClass('Zend_Gdata_AuthSub');

/**
 * @see Zend_Gdata_Photos
 */
Zend_Loader::loadClass('Zend_Gdata_Photos');

/**
 * @see Zend_Gdata_Photos_UserQuery
 */
Zend_Loader::loadClass('Zend_Gdata_Photos_UserQuery');

/**
 * @see Zend_Gdata_Photos_AlbumQuery
 */
Zend_Loader::loadClass('Zend_Gdata_Photos_AlbumQuery');

/**
 * @see Zend_Gdata_Photos_PhotoQuery
 */
Zend_Loader::loadClass('Zend_Gdata_Photos_PhotoQuery');

/**
 * @see Zend_Gdata_App_Extension_Category
 */
Zend_Loader::loadClass('Zend_Gdata_App_Extension_Category');



class PicasaSync extends AbtractPicSync
{
	public $client;
	public $user;
	
	protected  function getAuthSubHttpClient()
	{
    	global $_SESSION, $_GET,$client,$user;
    	if (!isset($_SESSION['sessionToken']) && isset($_GET['token'])) {
        $_SESSION['sessionToken'] =
            Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);
    	}	
    	$client = Zend_Gdata_AuthSub::getHttpClient($_SESSION['sessionToken']);
    	return $client;
	}
	
	protected function getCurrentUrl()
	{
    	global $_SERVER;

    	/**
     	* Filter php_self to avoid a security vulnerability.
     	*/
    	$php_request_uri = htmlentities(substr($_SERVER['REQUEST_URI'], 0,
    	strcspn($_SERVER['REQUEST_URI'], "\n\r")), ENT_QUOTES);

    	if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
	        $protocol = 'https://';
    	} else {
	        $protocol = 'http://';
	    }
    	$host = $_SERVER['HTTP_HOST'];
    	if ($_SERVER['SERVER_PORT'] != '' &&
        	(($protocol == 'http://' && $_SERVER['SERVER_PORT'] != '80') ||
        	($protocol == 'https://' && $_SERVER['SERVER_PORT'] != '443'))) {
	            $port = ':' . $_SERVER['SERVER_PORT'];
	    } else {
    	    $port = '';
    	}	
    	return $protocol . $host . $port . $php_request_uri;
	}
	
	
	protected function getAuthSubUrl()
	{
    	$next = $this->getCurrentUrl();
    	$scope = 'http://picasaweb.google.com/data';
    	$secure = false;
    	$session = true;
    	return Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope, $secure,$session);
	}
	
	protected function requestUserLogin($linkText)
	{
    	$authSubUrl = $this->getAuthSubUrl();
    	echo "<a href=\"{$authSubUrl}\">{$linkText}</a>";
	}
	
	function getSession() {
		if(!isset($_SESSION)){
			session_start();
		}
		global $_SESSION, $_GET,$client,$user;
    	if (!isset($_SESSION['sessionToken']) && !isset($_GET['token']))
    	{
        	$this->requestUserLogin('Please login to your Google Account.');
		}
		else {
			$user=$_REQUEST['user'];
			$client = $this->getAuthSubHttpClient(); 
		}
		return $client;
	}	
	
		public function getAlbums()
	{
		global $client,$user;
		$this->outputUserFeed($client,$user);
	}
	
	protected  function outputUserFeed($client, $user)
	{
    	$photos = new Zend_Gdata_Photos($client);
    	$query = new Zend_Gdata_Photos_UserQuery();
		$query->setUser($user);
		$userFeed = $photos->getUserFeed(null, $query);
		foreach ($userFeed as $entry) {
        	if ($entry instanceof Zend_Gdata_Photos_AlbumEntry) {
			echo $entry->getXML().'$$$$$$$$$$';
			}
		}
	}
	
	public function addAlbum($name)
	{
		global $client;
    	$photos = new Zend_Gdata_Photos($client);
    	$entry = new Zend_Gdata_Photos_AlbumEntry();
    	$entry->setTitle($photos->newTitle($name));

	    $result = $photos->insertAlbumEntry($entry);
	    if ($result) {
        	echo "album created successfully.";
    	} else {
        	echo "There was an issue with the album creation.";
    	}
    
	}
    
    
    public function deleteAlbum($albumId)
    {
    	global $client,$user;
    	$photos = new Zend_Gdata_Photos($client);
    	$albumQuery = new Zend_Gdata_Photos_AlbumQuery;
    	$albumQuery->setUser($user);
    	$albumQuery->setAlbumId($albumId);
    	$albumQuery->setType('entry');
    	$entry = $photos->getAlbumEntry($albumQuery);
    	$result = $photos->deleteAlbumEntry($entry, true);
    	
    	
    }
    
    public function uploadPhoto($albumId, $photo)
    {
    	global $client,$user;
    	$photos = new Zend_Gdata_Photos($client);

    	$fd = $photos->newMediaFileSource($photo["tmp_name"]);
    	$fd->setContentType($photo["type"]);

    	$entry = new Zend_Gdata_Photos_PhotoEntry();
    	$entry->setMediaSource($fd);
    	$entry->setTitle($photos->newTitle($photo["name"]));

    	$albumQuery = new Zend_Gdata_Photos_AlbumQuery;
    	$albumQuery->setUser($user);
    	$albumQuery->setAlbumId($albumId);

    	$albumEntry = $photos->getAlbumEntry($albumQuery);

    	$result = $photos->insertPhotoEntry($entry, $albumEntry);
    	if ($result) {
        	echo "Photo Uploaded successfully.";
    	} else {
	        echo "There was an issue with the file upload.";
    	}
    }
    
    public function  deletePhoto($albumId,$photoId)
    {
    	global $client,$user;
    	$photos = new Zend_Gdata_Photos($client);
	    $photoQuery = new Zend_Gdata_Photos_PhotoQuery;
    	$photoQuery->setUser($user);
    	$photoQuery->setAlbumId($albumId);
    	$photoQuery->setPhotoId($photoId);
    	$photoQuery->setType('entry');
    	$entry = $photos->getPhotoEntry($photoQuery);
	    $photos->deletePhotoEntry($entry, true);
    	
    }
}
	

?>
